How to create list in python?Give suitable example.
How to create list in python?Give suitable example.
584
28-Mar-2023
Aryan Kumar
20-Apr-2023In Python, you can create a list by enclosing a comma-separated sequence of items in square brackets [].
Here's an example of creating a list of numbers:
You can also create an empty list and add items to it later using square bracket notation []:
You can also use the list() constructor to create a list from a sequence, such as a string or a tuple:
Once you have created a list, you can access its items by using square bracket notation to specify the index:
Krishnapriya Rajeev
29-Mar-2023The list is one of the 4 built-in data types in Python, which is used to store multiple items in a single variable. Items in lists are ordered, mutable, and allow for duplicate values.
Example:
Lists can also contain items of different data types.
We can insert a new list item by using the insert() method.
To add items to the end of the list, we use append().
Lists are indexed, hence the items can be accessed by using an index number.